package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesChannelRankReportWriteRepository;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.dto.UpdateRankNoResponse;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.dto.UpdateResponse;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.model.MonthSalesChannelRankReport;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.service.MonthSalesChannelRankReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/26 21:49
 */
@Slf4j
@Component
public class MonthSalesChannelBankReportWriteRepositoryImpl implements MonthSalesChannelRankReportWriteRepository {

    @Resource
    MonthSalesChannelRankReportService monthSalesChannelRankReportService;

    @Override
    public String add(MonthSalesBasicReportSDO sdo) {
        MonthSalesChannelRankReport monthSalesChannelRankReport = new MonthSalesChannelRankReport();
        BeanUtils.copyProperties(sdo, monthSalesChannelRankReport);
        Map m = (Map)monthSalesChannelRankReportService.create(monthSalesChannelRankReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    @Override
    public Integer update(MonthSalesBasicReportSDO sdo) {
        MonthSalesChannelRankReport monthSalesChannelRankReport = new MonthSalesChannelRankReport();
        BeanUtils.copyProperties(sdo, monthSalesChannelRankReport);
        log.info("MonthSalesChannelBankReportWriteRepositoryImpl.update.sdo={}", JSON.toJSONString(sdo));
        UpdateResponse response = monthSalesChannelRankReportService.update(monthSalesChannelRankReport);
        return response.getCount();
    }

    @Override
    public Result addOrUpdate(MonthSalesBasicReportSDO sdo) {
        MonthSalesChannelRankReport monthSalesChannelRankReport = new MonthSalesChannelRankReport();
        BeanUtils.copyProperties(sdo, monthSalesChannelRankReport);
        UpdateResponse response = monthSalesChannelRankReportService.update(monthSalesChannelRankReport);
        if (response.getCount() == 0) {
            Map m = (Map)monthSalesChannelRankReportService.create(monthSalesChannelRankReport);
            Map result = (Map)m.get("result");
            return Result.success(response.getCount(), (String)result.get("lastInsertId"));
        }
        return Result.success(response.getCount());
    }

    @Override
    public Integer updateRankNo(MonthSalesBasicReportSDO sdo) {
        MonthSalesChannelRankReport monthSalesChannelRankReport = new MonthSalesChannelRankReport();
        BeanUtils.copyProperties(sdo, monthSalesChannelRankReport);
        UpdateRankNoResponse response = monthSalesChannelRankReportService.updateRankNo(monthSalesChannelRankReport);
        return response.getCount();
    }
}
